package org.sdnstproject.interlayer.OF10Protocal;

import org.projectfloodlight.openflow.types.MacAddress;
import org.projectfloodlight.openflow.types.U32;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

public class FuPortModV10 {
	private static final Logger log = LoggerFactory.getLogger(FuPortModV10.class);
    public byte version = (byte)1;
    public byte type = (byte)15;
    public short length = (short)32;//fixed value property length = 32
    public long xid = 0x0;
    public short portNo;//OFPort
    public long hwAddr;//MacAddress
    public long config;
    public long mask;
    public long advertise;
    
    public FuPortModV10() {
    	
    }
    
    public FuPortModV10 setXid(long xid) {
		this.xid = xid;
		return this;
	}
    
    public FuPortModV10 setField(String field, String value) {
		switch (field) {
		case "version":
			this.version = Byte.parseByte(value);
			break;
		case "type":
			this.type = Byte.parseByte(value);
			break;
		case "length":
			this.length = Short.parseShort(value);
			break;
		case "xid":
			this.xid = Long.parseLong(value);
			break;
		case "portNo":
			this.portNo = Short.parseShort(value);
			break;
		case "hwAddr":
			this.hwAddr = Long.parseLong(value);
			break;
		case "config":
			this.config = Long.parseLong(value);
			break;
		case "mask":
			this.mask = Long.parseLong(value);
			break;
		case "advertise":
			this.advertise = Long.parseLong(value);
			break;
		}
		return this;
    }
    
    public ByteBuf writeTo() {
		ByteBuf bb = Unpooled.directBuffer();
        // fixed value property version = 1
        bb.writeByte(version);
        // fixed value property type = 15
        bb.writeByte(type);
        // fixed value property length = 32
        bb.writeShort(length);
        bb.writeInt(U32.t(xid));
        bb.writeShort(portNo);
        this.gethwAddrObject(this.hwAddr).write6Bytes(bb);
        bb.writeInt(U32.t(config));
        bb.writeInt(U32.t(mask));
        bb.writeInt(U32.t(advertise));
        // pad: 4 bytes
        bb.writeZero(4);
        return bb;
    }
    
    public MacAddress gethwAddrObject() {
		return MacAddress.of(hwAddr);
	}
	
	public MacAddress gethwAddrObject(long hwAddr) {
		return MacAddress.of(hwAddr);
	}
	
	public MacAddress gethwAddrObject(String hwAddr) {
		return MacAddress.of(hwAddr);
	}
    
    @Override
    public String toString() {
        StringBuilder b = new StringBuilder("OFPortModVer10(");
        b.append("version=").append(version);
        b.append(", ");
        b.append("type=").append(type);
        b.append(", ");
        b.append("length=").append(length);
        b.append(", ");
        b.append("xid=").append(xid);
        b.append(", ");
        b.append("portNo=").append(portNo);
        b.append(", ");
        b.append("hwAddr=").append(hwAddr);
        b.append(", ");
        b.append("config=").append(config);
        b.append(", ");
        b.append("mask=").append(mask);
        b.append(", ");
        b.append("advertise=").append(advertise);
        b.append(")");
        return b.toString();
    }
    
}
